public class SetOrderNumberController
{
     public String OrderId
     {
           get
           {
               return String.valueOf(System.currentPageReference().getParameters().get('id')).substring(0,15);  
           }
           set;
     }
      public String ActivityId
     {
           get
           {
               return String.valueOf(System.currentPageReference().getParameters().get('ActivityId')).substring(0,15);  
           }
           set;
     }
       public String OutletId
     {
           get
           {
               return String.valueOf(System.currentPageReference().getParameters().get('OutletId')).substring(0,15);  
           }
           set;
     }
     public Boolean IsEnableSaveButton{get;set;}
     
     public List<Order_Item__c> getOrderItemList { get;set;}
       public SetOrderNumberController()
       {
           IsEnableSaveButton=true;
           getOrderItemList =LoadOrderItemList();
           for(Order__c order: [select id,DistributorConfirm__c from Order__c where id=:OrderId])
           {
               if(order.DistributorConfirm__c==true)
                   IsEnableSaveButton=false;
           }
        }
       public List<Order_Item__c> LoadOrderItemList()
       {
          List<Order_Item__c> orderList= [select id,name,Product_Name__c,OrderNumber__c,Unit__c,Package__c,DistributorSetOrderNumber__c,Resource_Allocation_Detail__c  
                                                               from Order_Item__c where Order_Name__c=:OrderId];
           return orderList;
       }
       
       public PageReference Save()
       {
           try
           {
               for(Order_Item__c oItem:getOrderItemList)
               {
                   if(oItem.DistributorSetOrderNumber__c>oItem.OrderNumber__c)
                       throw new CustomException('确认下单数不能超过实际下单数');
               }
           }
           catch(Exception ex)
           {
               ApexPages.addMessages(ex);
               return null;
           }
           update getOrderItemList;
           //更新状态
         //  Order__c order=[select id,DistributorConfirm__c from Order__c where id=:OrderId];
         //  order.DistributorConfirm__c =true;
       //    update order;
           //减去活动中相应的门店总数
       //    ModifyActivityTotalNumberAndOutletNumber();
            pageReference pr = new pageReference('/'+OrderId);     
             pr.setredirect(true);   
             return pr;
       }
       public PageReference Cancel()
       {
            pageReference pr = new pageReference('/'+OrderId);     
             pr.setredirect(true);   
             return pr;
       }
    /*   public void ModifyActivityTotalNumberAndOutletNumber()
       {
           //取得产品ID
         //  map<Id,Order_Item__c> productIdList=new map<Id,Order_Item__c>();
            set<string> resourceIdList=new set<string>();
           for(Order_Item__c oItem:getOrderItemList )
           {
               resourceIdList.add(oItem.Resource_Allocation_Detail__c);
           }
          map<id,Resource_Allocation_Detail__c> radList=new map<id,Resource_Allocation_Detail__c>([select id,ProductId__c,
                                                                                                                                                          HasUsedResourceNumber__c,Resource_Quantity_Number__c
                                                                                                                                                          from Resource_Allocation_Detail__c 
                                                                                                                                                          where id in:resourceIdList]);
                                                                                                                                                          
           for(Order_Item__c oItem:getOrderItemList )
           {
               if(radList.containsKey(oItem.Resource_Allocation_Detail__c))
               {
                   Resource_Allocation_Detail__c rad=radList.get(oItem.Resource_Allocation_Detail__c);
                    if(oItem.DistributorSetOrderNumber__c<oItem.OrderNumber__c)
                    {
                         rad.HasUsedResourceNumber__c-=oItem.OrderNumber__c-oItem.DistributorSetOrderNumber__c;
                         radList.put(oItem.Resource_Allocation_Detail__c,rad);
                    }
               }
           }
           update radList.values();
       }*/
}